package com.school.dao.impl;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.school.dao.TextBookDao;
import com.school.domain.Grade;
import com.school.domain.TextBook;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class TextBookDaoImpl implements TextBookDao {
    private QueryRunner queryRunner = new QueryRunner(new ComboPooledDataSource());
    @Override
    public List<TextBook> findAll() {
        List<TextBook> textBooks = null;
        try {
            textBooks = queryRunner.query("select * from grade", new BeanListHandler<>(TextBook.class));
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return textBooks;
    }

    @Override
    public boolean addTextBook(TextBook book) {
        int result;
        try {
            result = queryRunner.update("insert into textBook values(?, ?)", null, book.getName());

        } catch (SQLException throwables) {
            return false;
        }
        return result == 1;
    }
}
